﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class UICopyAward :Page
    {
        //星星奖励
        public UILabel awardSubmitBtnTxt;
        public UILabel awardStarTxt;

        public GameObject awardItem; //award panel
        public UIGrid awardItemGrid;
        public UIScrollView scrollView;

        public int currGetStar=0;

        public TweenAlpha titleTweenAlpha;
        public TweenPosition titleTweenPosition;
        public TweenAlpha maskTweenAlpha;
        public TweenScale bodyTweenScale;
        public TweenAlpha rotateAlpha;

        public GameObject bk;

        public GameObject buttom;
       
        void Awake()
        {
            awardSubmitBtnTxt.text =  TableTextBlo.GetText( "submit" );
            awardStarTxt.text = TableTextBlo.GetText( "copy_award_title" );
        }

        public string currentChapterId;
        List<GameObject> awardItems = new List<GameObject>();
        
        public void SetAwardGet(int index)
        {
            var obj = awardItems[index];
            var item = obj.GetComponent<UICopyAwardItem>();
            item.SetHasGet();
        }

        protected override void DoOpen()
        {
            base.DoOpen();

            buttom.SetActive( false );

            currentChapterId = options["chapterId"];
            StartCoroutine( _CreateItem() );
            currGetStar = UICopyData.GetChapterStar( currentChapterId );

            DoTweens();
            Invoke( "_ShowButton", titleTweenAlpha.duration + bodyTweenScale.duration );

        }

        void DoTweens()
        {
            titleTweenAlpha.ResetToBeginning();
            titleTweenPosition.ResetToBeginning();
            maskTweenAlpha.ResetToBeginning();
            bodyTweenScale.ResetToBeginning();
            rotateAlpha.ResetToBeginning();
            
            titleTweenAlpha.PlayForward();
            titleTweenPosition.PlayForward();
            maskTweenAlpha.PlayForward();
            bodyTweenScale.PlayForward();
            rotateAlpha.PlayForward();
        }

        void _ShowButton()
        {
            buttom.SetActive( true );
        }

        IEnumerator _CreateItem()
        {
            if( awardItems.Count>0)
            {
                foreach( var obj in awardItems )
                {
                    GameObject.DestroyImmediate( obj );
                }

                awardItems.Clear();
            }
            yield return new WaitForSeconds( titleTweenAlpha.duration + bodyTweenScale.duration );


            if( awardItems.Count==0 )
            {
                var chapterData = UICopyData.GetUIChapterData( currentChapterId );
                for( int i=0; i<chapterData.chapter_box_condition.Length; ++i )
                {
                    GameObject obj = GameObject.Instantiate( awardItem ) as GameObject;
                    obj.name = i.ToString();
                    obj.SetActive( true );
                    obj.transform.SetParent( awardItemGrid.transform );
                    obj.transform.localScale = Vector3.one;

                    var awardIt = obj.GetComponent<UICopyAwardItem>();
                    var starCount =chapterData.chapter_box_condition[i];
                    var starStr=chapterData.chapter_award_box[i];

                    awardIt.Init( starCount, i, new UIDropData( starStr ),GetComponent<UICopyAwardEvent>(),this);

                    awardIt.alpha.ResetToBeginning();
                    awardIt.alpha.PlayForward();

                    if (currGetStar >= starCount) //达成了
                    {
                        var hasGet = false;
                        if (i == 0)
                        {
                            var hasGet0 = chapterData.chapter_inst_get_box_0;
                            if (hasGet0)
                            {
                                awardIt.SetHasGet();
                                hasGet = true;
                            }
                        }
                        else if (i == 1)
                        {
                            var hasGet1 = chapterData.chapter_inst_get_box_1;
                            if (hasGet1)
                            {
                                awardIt.SetHasGet();
                                hasGet = true;
                            }
                        }
                        else if (i == 2)
                        {
                            var hasGet2 = chapterData.chapter_inst_get_box_2;
                            if (hasGet2)
                            {
                                awardIt.SetHasGet();
                                hasGet = true;
                            }
                        }

                        if(!hasGet)
                        {
                            awardIt.SetCanGet();
                        }
                    }
                    else
                    {
                        awardIt.SetCanNotGet();
                    }
                    
                    awardItems.Add( obj );
                    awardItemGrid.Reposition();
                    yield return null;
                }
            }

            scrollView.ResetPosition();
        }

        protected override void DoClose()
        {
            StopAllCoroutines();
            if( awardItems.Count>0 )
            {
                foreach( var obj in awardItems )
                {
                    GameObject.DestroyImmediate( obj );
                }

                awardItems.Clear();
            }
        }
    }
}